package com.wanyu.video.nio.channel;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 使用NIO读取文件内容
 */
public class NIOFileChannelReadFile {

    public static void main(String[] args) throws IOException {

        FileInputStream fileInputStream = new FileInputStream("C:\\git_code\\netty-in-action-learn\\file\\file02.txt");

        // 获取文件channel
        FileChannel inputStreamChannel = fileInputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(20);

        // 读取通道中的数据，写入到缓冲区
        inputStreamChannel.read(byteBuffer);


        // 反转buffer开始进行读取操作
        byteBuffer.flip();

        System.out.println(new String(byteBuffer.array()));

        inputStreamChannel.close();
        fileInputStream.close();
    }
}
